home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacWorld 1998 May
/
Macworld (1998-05).dmg
/
Shareware World
/
Comms & Internet
/
ImageMapper v3.0
/
Tutorial
/
java
/
Source
/
HighlightedImageMap.java
< prev
Wrap
Text File
|
1998-03-08
|
7KB
|
283 lines
/**
*
* HighlightedImageMap
*
* This applet was originally developed by Rickard Oberg (rickard@lysator.liu.se)
*
* Modifications were made by Stuart Snaddon (snaddosg@dcs.gla.ac.uk) to make the applet:
* • More stable (crashes were reported with the original applet)
* • Capable of playing sounds if required
* • Show the 'ALT' text in the browser status bar (if both exist!)
*
* These modifications are intended to make the applet more useful with the macintosh
* application ImageMapper v3.0 (shareware $15), but with the original spirit of Rickard's applet,
* this code has been placed in the public domain, feel free to modify and distribute, but please
* keep this notice intact.
*
*/
import java.applet.*;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class HighlightedImageMap extends Applet
{
Image normalimg;
Image highlightedimg;
public boolean nloaded=false;
public boolean hloaded=false;
boolean debug=false;
public void start()
{
// Check if the images are already loaded (Cache fix)
if ((checkImage(normalimg, this) & ImageObserver.ALLBITS)==ImageObserver.ALLBITS)
{
nloaded = true;
}
if ((checkImage(highlightedimg, this) & ImageObserver.ALLBITS)==ImageObserver.ALLBITS)
{
hloaded = true;
}
// Paint it!
repaint();
}
public void init()
{
//Applet name
System.out.println("\nThis applet is a modified, enhanced version of:");
System.out.println("HighlightedImageMap v1.11, by Rickard ÷berg(rickard@lysator.liu.se) 1996");
System.out.println("\nPart of ImageMapper v3.0");
System.out.println("(c)1997 Stuart Snaddon(snaddosg@kagi.com)\n");
//Show "Loading"
repaint();
//Show debug(X/Y) info?
debug=(getParameter("debug")==null)?false:true;
//Target frame name. If null then same frame is target
//Either one target for every link, or one for each link
String target=getParameter("target");
if (target==null)
target="";
boolean individualtargets=false; //If true, each link has it's own target frame/window
StringBufferInputStream tsbis=new StringBufferInputStream(target);
StreamTokenizer tst=new StreamTokenizer(tsbis);
if (target.indexOf(',')!=-1)
individualtargets=true; //Individual link targets
//Get images
URL normalurl, highlightedurl;
try
{
normalurl=new URL(getDocumentBase(),getParameter("normal"));
highlightedurl=new URL(getDocumentBase(),getParameter("highlight"));
}
catch(Exception e)
{
System.out.println("Error: 'normal' or 'highlight' not valid");
return;
}
normalimg = getImage(normalurl);
highlightedimg = getImage(highlightedurl);
prepareImage(normalimg, this);
prepareImage(highlightedimg, this);
setLayout(null);
repaint();
resize(normalimg.getWidth(this), normalimg.getHeight(this));
/*
*
* Parse links
*
*/
String links=getParameter("links");
//No links, no imagemap
if (links==null)
{
System.out.println("Error: no links specified");
return;
}
StringBufferInputStream sbis=new StringBufferInputStream(links);
StreamTokenizer st=new StreamTokenizer(sbis);
st.quoteChar(39);
//Syntax {ulx,uly,lrx,lry,"URL"}
int x,y,w,h;
URL url = null, audioURL = null; //Link URLs
String alt;
boolean done=false; //Done parsing links?
try
do
{
//Default alternative text
alt="";
if (st.nextToken()!='{') throw new Exception("{ expected");
//Upper left x
if (st.nextToken()!=StreamTokenizer.TT_NUMBER) throw new Exception("number expected");
x=(int)st.nval;
// ','
if (st.nextToken()!=',') throw new Exception("',' expected");
//Upper left y
if (st.nextToken()!=StreamTokenizer.TT_NUMBER) throw new Exception("number expected");
y=(int)st.nval;
// ','
if (st.nextToken()!=',') throw new Exception("',' expected");
//Lower right x
if (st.nextToken()!=StreamTokenizer.TT_NUMBER) throw new Exception("number expected");
w=(int)st.nval-x;
// ','
if (st.nextToken()!=',') throw new Exception("',' expected");
//Lower right y
if (st.nextToken()!=StreamTokenizer.TT_NUMBER) throw new Exception("number expected");
h=(int)st.nval-y;
// ','
if (st.nextToken()!=',') throw new Exception("',' expected");
//URL
if (st.nextToken()!=39) throw new Exception("quoted url expected(use ' for quotation)");
url=new URL(getDocumentBase(),st.sval);
//Optional alternative text
if (st.nextToken()==',')
{
if (st.nextToken()==39)
alt=st.sval;
else
throw new Exception("alternative linktext expected");
}
else
st.pushBack();
//Optional AudioClip URL
if (st.nextToken()==',')
{
if (st.nextToken()==39)
audioURL=new URL(getDocumentBase(),st.sval);
else
throw new Exception("quoted url expected(use ' for quotation)");
}
else
st.pushBack();
if (st.nextToken()!='}')
throw new Exception("} expected");
//Get target name
String linktarget; //Target for this particular link
if (individualtargets)
{
tst.nextToken();
linktarget=tst.sval;
tst.nextToken();
}
else
linktarget=target; //Use global target
//Create images for link
ImageFilter cropfilter = new CropImageFilter(x,y,w,h);
Image nimg = createImage(new FilteredImageSource(normalimg.getSource(),
cropfilter));
Image hlimg = createImage(new FilteredImageSource(highlightedimg.getSource(),
cropfilter));
//Add link
add(new HighlightableLink(x, y, w, h, nimg, hlimg, url, audioURL, alt, linktarget, this));
//Check if end of links
if (st.nextToken()==StreamTokenizer.TT_EOF)
done=true;
st.pushBack();
} while(!done);
catch(Exception e)
{
System.out.println("Error in links parameter:");
System.out.println(e);
}
}
public void paint(Graphics g)
{
if (nloaded && hloaded)
{
g.drawImage(normalimg,0,0,this);
}
else
{
g.drawString("Loading",0,10);
}
paintComponents(g);
}
public void update(Graphics g)
{
paint(g);
}
public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height)
{
if (img==normalimg && (infoflags & ImageObserver.ALLBITS)==ImageObserver.ALLBITS)
{
nloaded = true;
}
if (img==highlightedimg && (infoflags & ImageObserver.ALLBITS)==ImageObserver.ALLBITS)
{
hloaded = true;
}
if (nloaded && hloaded)
repaint();
return !(hloaded && nloaded);
}
public boolean mouseMove(Event evt, int x, int y)
{
//Show debug(X/Y) info
if (debug)
{
showStatus("X: "+x+" Y: "+y);
return true;
}
return false;
}
}